Page({
	data: {

		inputCode: '',
		isScan: false,
		isShow: false,
		obj: null,
		urlAddress: getApp().globalData.imageUrl,
		isEmpty: true

	},
	onLoad: function(options) {

		this.getShopMarket();
	},
	onReady: function() {

	},
	onShow: function() {

	},
	onHide: function() {

	},
	onUnload: function() {

	},
	getShopMarket: function() {
		var _self = this;
		wx.showLoading({
			title: '加载中',
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/cart/getCartGoods.do',
			data: {
				shopCode: getApp().globalData.common.shopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				wx.hideLoading()
				_self.setData({
					obj: res.data.obj
				});
			}
		})
	},
	showInput: function() {
		this.setData({
			isShow: !this.data.isShow
		});
	},
	cancelAction: function() {
		this.setData({
			isShow: !this.data.isShow
		});

	},
	CodeInput: function(e) {
		this.setData({
			inputCode: e.detail.value
		})
	},
	makeSure: function() {
		if (this.data.inputCode == '') {

		} else {
			var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]")
			if (!pattern.test(this.data.inputCode)) {
				this.addShopMarket(this.data.inputCode, 1);
			} else {
				wx.showToast({
					title: '非商品条码',
					image: '../../image/error.png',
					duration: 2000
				})
			}
		}

	},
	addShopMarket: function(code, num) {
		var _self = this;
		wx.showLoading({
			title: '加载中',
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/cart/addGoods.do',
			data: {
				gbk: code,
				shopCode: getApp().globalData.common.shopCode,
				num: num
			},
			header: getApp().globalData.header,
			success: function(res) {
				wx.hideLoading();
				_self.setData({
					obj: res.data.obj,
					isShow: false
				});
				// console.log(res.data.obj.lastGoodsisNull)
				if (res.data.obj.lastGoodsisNull & num == 1) {
					wx.showToast({
						title: '没有该商品',
						image: '../../image/error.png',
						duration: 2000
					})
				}


			}
		})
	},
	addAction: function(e) {
		this.addShopMarket(e.currentTarget.dataset.code, -1);
	},
	subAction: function(e) {

		this.addShopMarket(e.currentTarget.dataset.code, 1);
	},
	submitAction: function() {
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.navigateTo({
			url: '../pay/pay'
		})
	},
	cleanAll: function() {
		if (this.data.obj) {
			var _self = this;
			wx.showLoading({
				title: '加载中',
			})
			wx.request({
				url: getApp().globalData.baseUrl + '/applet/cart/clear.do',
				data: {
					shopCode: getApp().globalData.common.shopCode,
				},
				header: getApp().globalData.header,
				success: function(res) {
					wx.hideLoading()
					_self.setData({
						obj: res.data.obj,
					});
				}
			})
		}
	},
	scanAction: function() {
		var _self = this;
		wx.scanCode({
			success: (res) => {
				var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]")

				if (!pattern.test(res.result)) {
					_self.addShopMarket(res.result, 1);
				} else {
					_self.showMyToast(_self, "非商品条码")
				}
			}
		})
	},
	showMyToast: function(self, my_toast_tip, time = 1000, template = 'my_toast') {
		self.setData({
			template: template, //my_toast
			my_toast_tip: my_toast_tip
		})
		var timer = setTimeout(() => {
			self.setData({
				template: '', //my_toast
				my_toast_tip: ''
			})
			clearTimeout(timer);
		}, time)

	},
	/// 长按
	longTap: function(e) {
		var _self = this;
		wx.showModal({
			title: '提示',
			content: '是否删除' + e.currentTarget.dataset.name + '商品',
			showCancel: true,
			success: function(res) {
				if (res.confirm) {
					wx.request({
						url: getApp().globalData.baseUrl + '/applet/cart/delete.do',
						data: {
							shopCode: getApp().globalData.common.shopCode,
							goodsCode: e.currentTarget.dataset.code
						},
						header: getApp().globalData.header,
						success: function(res) {
							wx.hideLoading()
							_self.setData({
								obj: res.data.obj,
							});
						}
					})
				}
			}
		})
	}

})